<template>
  <div style="padding:20px;">
    <!-- 条件查询的表单：收集查询条件 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <!-- 收集的数据查询时 在后端会作为查询sql的条件进行使用，为了前后照应，建议前端的数据的key和后端数据库表对应bean的字段一样 -->
        <el-input v-model="searchObj.name" placeholder="讲师"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchObj.level" placeholder="讲师头衔">
          <el-option label="高级讲师" value="1"/>
          <el-option label="首席讲师" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          v-model="searchObj.joinDateBegin"
          type="date"
          placeholder="起始日期"/>
        --
        <el-date-picker
          v-model="searchObj.joinDateEnd"
          type="date"
          placeholder="截止日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryByCondition">查询</el-button>
        <el-button @click="clear">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面标签内容 -->
    <el-table
      :data="teachers"
      style="width: 100%">
      <el-table-column
        type="index"
        label="编号"
        width="50">
        <template>
          {{ this.$index }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="150"/>
      <el-table-column
        prop="name"
        label="头像"
        width="80">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="50px" height="50px">
        </template>
      </el-table-column>
      <el-table-column
        prop="career"
        show-overflow-tooltip
        label="资历"/>
      <el-table-column
        prop="level"
        label="讲师头衔"
        width="200"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.level===1?'success':'danger'">{{ scope.row.level===1?'高级讲师':'首席讲师' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        width="80"/>
      <el-table-column
        prop="joinDate"
        width="200"
        label="入驻时间"/>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align:center;margin-top:200px">
      <el-pagination
        :current-page="pageNum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>
<script>
// js脚本
import teacherApi from '@/api/teacher'
export default {
// 相当于在vue对象中写代码

  data() {
    return {
      teachers: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
      searchObj: {},
      teacher: {}
    }
  },
  created() {
    // this.getTeachers()// 中午忘了在这调函数
    this.queryByCondition()
  },
  methods: {
    handleEdit(idx, teacher) {
      // 跳转到form，并携带id
      /*
        路径参数:/teacher/edit/id
        请求参数:/teacher/edit?id=xxx      会在请求路径中暴露出来 不好
      */
      // this.$router.push('/teacher/edit?id=' + teacher.id)// id传到了所跳转页面的路由的$route.query.id
      this.$router.push('/teacher/edit/' + teacher.id) // 用$route.params.id接收
    },
    // 清空查询表单
    clear() {
      this.searchObj = {}
    },
    // 根据条件分页查询
    queryByCondition() {
      teacherApi.pageByCondition(this.pageNum, this.pageSize, this.searchObj).then(res => {
        this.teachers = res.data.page.records
        this.total = res.data.page.total
        this.clear()
      })
    },
    // 查询所有teacher
    getTeachers() {
      teacherApi.list().then(res => {
        this.teachers = res.data.list// res在request里设定过，就是后端所返回的R对象。
      })
    },
    // 页面pageSize改变
    handleSizeChange(size) {
      console.log('size = ' + size)
      this.pageSize = size
      this.queryByCondition()
    },
    // 切换页码
    handleCurrentChange(num) {
      console.log('num = ' + num)
      this.pageNum = num
      this.queryByCondition()
    },
    // 分页查询
    getPage() {
      teacherApi.page(this.pageNum, this.pageSize).then(res => {
        this.teachers = res.data.page.records
        this.total = res.data.page.total
      })
    },
    // 删除teacher
    handleDelete(index, teacher) {
      this.$confirm(`此操作将永久删除 ${teacher.name}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除api方法 传入要删除的讲师id
        teacherApi.deleteById(teacher.id).then(r => {
          this.$message.success('删除成功')
          // 刷新页面
          this.queryByCondition()
        })
      }).catch(() => { // teacher.js中没写return，所以上面.then报异常一直跳下来。
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
